#!/usr/bin/python
# -*- encoding: utf-8 -*-
'''
@File    : main.py
@Desc    : 
@Version : 1.0
@Author  : echohelper 
@Email   : echohelper@qq.com
@WebSite : www.github.com/echohelper
@Time    : 2021/05/06 11:47:14
'''
# Start typing your code from here

import sys
import os
import importlib
import json
import xml.sax
import xml.etree.ElementTree as ET
from xml.dom.minidom import parse
import xml.dom.minidom as xmldom

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class GMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUi()
        self.tabWidget = QTabWidget()
        # self.tabWidget.setTabsClosable(True)
        self.setCentralWidget(self.tabWidget)

    def initUi(self):
        self.initMenuBar()
        self.initStatusBar()

        self.resize(1024, 666)
        self.setWindowTitle('Tinkit')
        self.setWindowIcon(QIcon('logo.ico'))
        self.show()

    def initMenuBar(self):
        menubar = self.menuBar()

        absXmlFilePath = os.path.abspath("menu.xml")
        tree = ET.parse(absXmlFilePath)
        root = tree.getroot()
        self.createXmlMenu(root, menubar)

    def initStatusBar(self):
        self.statusBar().showMessage('tinkit. you do it. it for you.')

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def closeEvent(self, event):
        reply = QMessageBox.Yes

        #reply = QMessageBox.question(self, 'Message',
        #	"Are You Sure To Quit ?", QMessageBox.Yes |
        #	QMessageBox.No, QMessageBox.Yes)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

    def onMenuClicked(self):
        sender = self.sender()
        cmdId = sender.objectName()
        cmdName = sender.text()

        cmdTip = "Click Cid：" + cmdId + " Name：" + cmdName
        print(cmdTip)

        package = 'module.childui'
        module = cmdId
        fullName = package + '.' + module
        try:
            objPkg = __import__(fullName, fromlist=True)
            
            funcName = 'getWidget'
            if hasattr(eval('objPkg.' + cmdId), funcName):
                func = getattr(eval('objPkg.' + cmdId), funcName)
                widget = func(eval('objPkg.' + cmdId)())
                widget.setWindowTitle(cmdName)
                self.tabWidget.addTab(widget, cmdName)
                self.tabWidget.setCurrentWidget(widget)
                print(widget)
            else:
                print('funcName in module 404 ' + funcName)

        except ImportError:
            print('don`t find module')
            self.statusBar().showMessage('don`t find module: ' + fullName, 5000)

    def onReadXmlFile(self):
        self.readXmlFile("menu.xml")
        self.statusBar().showMessage('Read Menu File Finished', 5000)

    def onWriteXmlFile(self):
        sender = self.sender()
        self.statusBar().showMessage(sender.text(), 5000)

    def createXmlMenu(self, element, parentMenu):
        #print (len(element))
        if len(element) > 0:
            for child in element:
                #print (child.tag, "---", child.attrib)
                if len(child) > 0:
                    newMenu = parentMenu.addMenu(QIcon(child.attrib.get("Icon")), child.attrib.get("Name"))
                    self.createXmlMenu(child, newMenu)
                else:
                    newAct = parentMenu.addAction(QIcon(child.attrib.get("Icon")), child.attrib.get("Name"))
                    newAct.setObjectName(child.attrib.get("Cmd"))
                    newAct.triggered.connect(self.onMenuClicked)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = GMainWindow()
    sys.exit(app.exec_())
